package com.key.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.key.modules.dto.CustomerStDTO;
import com.key.modules.entity.CarrierSt;
import com.key.modules.entity.CustomerSt;
import com.key.modules.mapper.CustomerStMapper;
import com.key.modules.service.CustomerStService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class CustomerStServiceImpl implements CustomerStService {
    @Autowired
    CustomerStMapper customerStMapper;

    @Override
    public Map<String, Object> findCustomerStatement(CustomerStDTO customerStDTO) {
        log.info("进入findCustomerReceivable方法，查找信息（支持高级搜索）"+
                "，CurrentPage = "+customerStDTO.getCurrentPage()+
                "，pageSize = "+customerStDTO.getPageSize()+
                "，CustomerName = "+customerStDTO.getCustomerName()+
                "，CreationTime = "+customerStDTO.getCreationTime()+
                "，BillingStatus= "+customerStDTO.getBillingStatus()
        );
        //设置paage分页查询
        Page<CustomerSt> page=new Page<>(customerStDTO.getCurrentPage(),customerStDTO.getPageSize());
        LambdaQueryWrapper<CustomerSt> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(customerStDTO.getCustomerName()), CustomerSt::getCustomerName, customerStDTO.getCustomerName())
                .eq(StringUtils.isNotEmpty(customerStDTO.getCreationTime()), CustomerSt::getCreationTime, customerStDTO.getCreationTime())
                .eq(customerStDTO.getBillingStatus()!=null, CustomerSt::getBillingStatus, customerStDTO.getBillingStatus());
//        QueryWrapper<CustomerSt> queryWrapper= new QueryWrapper<>();
//        queryWrapper.eq("customer_name",customerStDTO.getCustomerName())
//                .eq("creation_time",customerStDTO.getCreationTime())
//                .eq("billing_status",customerStDTO.getBillingStatus());

        Page<CustomerSt> reVOPage= customerStMapper.selectPage(page,lambdaQueryWrapper);
        //将获取的page里的total与数据返回
        List<CustomerSt> voList=reVOPage.getRecords();
        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("data",voList);
        Integer total=(int)page.getTotal();
        resultMap.put("total",total);

        log.info("findCustomerStatement方法结束返回数据");
        return resultMap;
    }

    @Override
    public int deleteStatement(String no) {
        QueryWrapper<CustomerSt> lambdaQueryWrapper=new QueryWrapper<>();
        lambdaQueryWrapper.eq("statement_number",no);
        int i=customerStMapper.delete(lambdaQueryWrapper);
        return i;
    }

    @Override
    public int editStatement(CustomerSt customerSt) {
        LambdaQueryWrapper<CustomerSt> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(customerSt.getStatementNumber()),CustomerSt::getStatementNumber,customerSt.getStatementNumber());
        int i=customerStMapper.update(customerSt,lambdaQueryWrapper);
        return i;
    }
}
